import { init } from 'l2d';


document.body.insertAdjacentHTML('beforeend', `
    <canvas id="app"></canvas>
`);

const app = init(document.getElementById('app') as HTMLCanvasElement);
app
    .create({
        path: './hiyori/hiyori_pro_t11.model3.json',
    });

document.body.insertAdjacentHTML('beforeend', `
    <div id="chat"></div>
`);


const queues: Array<[string, ArrayBuffer]> = [];

function speak() {
    const [text, buffer] = queues.shift()!;
    const blob = new Blob([buffer], { type: 'audio/wav' });
    const audio = new Audio(URL.createObjectURL(blob));
    audio.play();
    audio.onclose = () => {
        if (queues.length > 0) {
            speak();
        }
    };
    const chat = document.getElementById('chat') as HTMLDivElement;
    chat.innerText = text;
}

ipcRenderer.on('speak', (e: any, text: string, buffer: ArrayBuffer) => {
    queues.push([text, buffer]);
    if (queues.length === 1) {
        speak();
    }
});